package com.cskt.erptemplate.controller;

import cn.hutool.core.codec.Base64;
import com.cskt.erptemplate.pojo.*;
import com.cskt.erptemplate.pojo.LoginVo;
import com.cskt.erptemplate.pojo.MenuDto;
import com.cskt.erptemplate.pojo.result.Result;
import com.cskt.erptemplate.pojo.result.auth.GenerateCaptchaDto;
import com.cskt.erptemplate.utils.MathUtils;
import com.google.code.kaptcha.Producer;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;


/**
 * @Author 陈厚德
 * @Version 2.2
 */

@RestController
@Slf4j
public class AuthController {


    @Resource
    private Producer producer;



    /**
     * 获取登录验证码
     */
    @GetMapping("/auth/captcha")
    public Result<GenerateCaptchaDto> generateCaptcha()  {

        String code = producer.createText();
        BufferedImage image = producer.createImage(code);

        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", os);
        } catch (IOException e) {
            throw new RuntimeException("验证码生成失败，请稍后重试！");
        }

        String sn = MathUtils.random();//随机四位树


        //将验证码存至redis
//        redisHandler.set(StringUtil.format(StringPool.LOGIN_CAPTCHA_KEY, sn), code,
//                kaptchaProperties.getExpireTime() * 60 * 1000L);

        GenerateCaptchaDto resp = new GenerateCaptchaDto();
        resp.setSn(sn);
        resp.setImage("data:image/jpeg;base64," + Base64.encode(os.toByteArray()));

        log.debug("获取验证码成功, SN={}, code={}", sn, code);

        return Result.ok(resp);

    }


    //暂时写死

    @PostMapping("/auth/login")
    public Result<LoginBo> login() {

        System.out.println("进来了");

        return Result.ok(new LoginBo());
    }



    //    退出登录  暂时写死
    @PostMapping("/auth/logout")
    public Result<Void> logout() {

//        AbstractUserDetails user = getCurrentUser();
//
//        String token = null;
//        if (user != null) {
//            token = userTokenResolver.getToken();
//        }
//
//        StpUtil.logout();
//
//        if (user != null) {
//            ApplicationUtil.publishEvent(new LogoutEvent(this, user, token));
//        }

        return Result.ok();
    }



    //获取用户信息  暂时写死值，不报错
    @GetMapping("/auth/info")
    public Result<LoginDto> info() {

        Set<String> set1= Collections.singleton("admin");

        LoginDto info = new LoginDto(null, "系统管理员", set1);

        return Result.ok(info);
    }



    //暂时写死值   不报错能运行
    @GetMapping("/auth/menus")
    public Result<List<MenuDto>> menus() {

        return Result.ok(null);
    }



}
